#include "GameUILayer.h"
#include "TitleScene.h"

bool GameUILayer::init()
{
	bool bRet = false;
	do 
	{
		// menu
		CCMenuItemImage *pPauseMenuItem = CCMenuItemImage::create(
			"images/interface/Button.png",
			"images/interface/ButtonSelected.png",
			this,
			menu_selector(GameUILayer::menuPauseCallback));
		CC_BREAK_IF(! pPauseMenuItem);

		CCPoint cpMenubgPause = ccp(VisibleRect::rightTop().x-120,VisibleRect::rightTop().y);
		pPauseMenuItem->setAnchorPoint(RectLayout::TopRight());
		pPauseMenuItem->setPosition(cpMenubgPause);

		CCMenuItemImage *pTitleBackMenuItem = CCMenuItemImage::create(
			"images/interface/Button.png",
			"images/interface/ButtonSelected.png",
			this,
			menu_selector(GameUILayer::menuTitleBackCallback));
		CC_BREAK_IF(! pTitleBackMenuItem);

		CCPoint cpMenubgTitle = VisibleRect::rightTop();
		pTitleBackMenuItem->setAnchorPoint(RectLayout::TopRight());
		pTitleBackMenuItem->setPosition(cpMenubgTitle);

		CCMenu* pMenu = CCMenu::create(pPauseMenuItem,pTitleBackMenuItem, NULL);
		pMenu->setVisible(false);
		CC_BREAK_IF(! pMenu);

		pMenu->setPosition(CCPointZero);
		this->addChild(pMenu,kMenu,kMenu);

		CCLabelTTF* pLabelButtonPause = CCLabelTTF::create("Pause", "Arial", 18);
		CC_BREAK_IF(! pLabelButtonPause);

		CCLabelTTF* pLabelButtonTitle = CCLabelTTF::create("Title", "Arial", 18);
		CC_BREAK_IF(! pLabelButtonTitle);

		CCPoint cpPause = ccp(VisibleRect::rightTop().x-163,VisibleRect::rightTop().y-14);
		pLabelButtonPause->setAnchorPoint(RectLayout::TopRight());
		pLabelButtonPause->setPosition(cpPause);
		pLabelButtonPause->setVisible(false);
		this->addChild(pLabelButtonPause,kMenuItemPause,kMenuItemPause);

		CCPoint cpTitleBack = ccp(VisibleRect::rightTop().x-33,VisibleRect::rightTop().y-14);
		pLabelButtonTitle->setAnchorPoint(RectLayout::TopRight());
		pLabelButtonTitle->setPosition(cpTitleBack);
		pLabelButtonTitle->setVisible(false);
		this->addChild(pLabelButtonTitle,kMenuItemBackTitle,kMenuItemBackTitle);

		CCNode* pmn = this->getChildByTag(kMenu);
		pmn->setVisible(true);

		CCNode* pmip = this->getChildByTag(kMenuItemPause);
		pmip->setVisible(true);

		CCNode* pmibt = this->getChildByTag(kMenuItemBackTitle);
		pmibt->setVisible(true);

	bRet = true;

	} while (0);

	return bRet;
}

void GameUILayer::menuPauseCallback( CCObject* pSender )
{
	if(!GameSetting::isGameStart) return;

	if( CCDirector::sharedDirector()->isPaused()){
		CCDirector::sharedDirector()->pause();
	}else{
		CCDirector::sharedDirector()->resume();
	}
}

void GameUILayer::menuTitleBackCallback( CCObject* pSender )
{
	CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::create(0.2f, TitleScene::scene()));
}
